//
//  PlayViewController.m
//  Everest
//
//  Created by 李其昌 on 14/12/17.
//  Copyright (c) 2014年 ThreeL. All rights reserved.
//

#import "PlayViewController.h"
#import "PlayViewController.h"
#import "UIImageView+WebCache.h"
#import "NetHandler.h"
#import "HotPresent.h"
#import "AvReadioPlayer.h"
#import "Album.h"
#import "UserInfo.h"
#import "ComprehensiveAlbum.h"
#import "MyNavigationView.h"
#import "PlayCell.h"
#import "UserInfo.h"
#import "DetailsCell.h"
#import "HotCell.h"
#import "UIColor+AddColor.h"
#import "FocusFetail.h"
#import "FoThreeCell.h"
#import "AlbumTableViewCell.h"
#import "NetHandleData.h"
#import "FileModel.h"
#import "JingRoundView.h"
#define kFoThreeReuse @"FoReuse"
#define kAlbumCell @"albumCell"
#define kHotReuse @"hotreuse"
#define REUSE @"reuse"
#define kReuse @"userReuse"


@interface PlayViewController ()<UITableViewDataSource, UITableViewDelegate>
// 表视图
@property(nonatomic, retain)UITableView *tableView;
// 专辑中得音频数组
@property(nonatomic, retain) NSMutableArray *tracksArr;

@property (nonatomic, retain) NSString *playUrl;

@property (nonatomic, retain) ComprehensiveAlbum *albumMore;

@property (nonatomic, retain) NSString *urlStr;

@property (nonatomic, strong) UISlider *volumeSlider;

@property (nonatomic, strong) UISlider *timeValueSlider;

@property (nonatomic, retain) UIView *playView;

@property (nonatomic, strong) UILabel *timerLabel;

@property (nonatomic, retain) UIButton *playButton;

@property (nonatomic, retain) UIButton *setUpButton;

@property (nonatomic, retain) UIButton *setDownButton;

@property (nonatomic, retain) UserInfo *userInfo;

@property (nonatomic, assign) NSInteger integer;

@property (nonatomic, retain) UIView *smallView;

@end

@implementation PlayViewController

// 内存管理
- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self.avPlayer];
    [_focusFetailArr release];
    [_focusFetail release];
    [_userInfo release];
    [_hotPlayArr release];
    [_playButton release];
    [_timerLabel release];
    [_playView release];
    [_timeValueSlider release];
    [_volumeSlider release];
    [_nav release];
    [_urlStr release];
    [_albumMore release];
    [_playUrl release];
    [_myImageView release];
    [_avPlayer release];
    [_albumPlay release];
    // 专辑
    [_album release];
    // 表视图
    [_tableView release];
    // 专辑中得音频数组
    [_tracksArr release];
    [_hotPlay release];
    [super dealloc];
}


+ (PlayViewController *)defaultPlayVC
{
    static PlayViewController *playVC = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        playVC = [[PlayViewController alloc] init];
    });
    return playVC;
}

- (void)setAlbum:(AlbumTracks *)album
{
    if (![self.playUrl isEqualToString:album.playUrl64]) {
        [self.avPlayer pause];
        [self.avPlayer playWithUrl:[NSURL URLWithString:album.playUrl64]];
        UIImage *image = [UIImage imageNamed:@"iconfont-zantinganniu.png"];
        [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        // 把播放图标背景换成暂停图标
        [self.playButton setImage:image forState:UIControlStateNormal];
        // 设置按钮的实现方法
        [self.playButton addTarget:self action:@selector(pauseButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
        
    }
    if (_album != album) {
        [_album release];
        _album = [album retain];
        self.playUrl = self.album.playUrl64;
        self.nav.label.text = self.album.title;
        [self.myImageView setImageWithURL:[NSURL URLWithString:_album.coverLarge] placeholderImage:[UIImage imageNamed:@"15.jpg"]];
        JingRoundView *jingRound = [JingRoundView defaultJingRoundView];
        [jingRound.roundImageView setImageWithURL:[NSURL URLWithString:_album.coverLarge] placeholderImage:[UIImage imageNamed:@"15.jpg"]];
    }
    [self handleUserData:[_album.trackId integerValue]];

}

- (void)setFocusFetail:(FocusFetail *)focusFetail
{
    if (![self.playUrl isEqualToString:focusFetail.playPath64]) {
        [self.avPlayer pause];
        [self.avPlayer playWithUrl:[NSURL URLWithString:focusFetail.playPath64]];
        UIImage *image = [UIImage imageNamed:@"iconfont-zantinganniu.png"];
        [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        // 把播放图标背景换成暂停图标
        [self.playButton setImage:image forState:UIControlStateNormal];
        // 设置按钮的实现方法
        [self.playButton addTarget:self action:@selector(pauseButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
    }
    if (_focusFetail != focusFetail) {
        [_focusFetail release];
        _focusFetail = [focusFetail retain];
        self.playUrl = self.focusFetail.playPath64;
        self.nav.label.text = self.focusFetail.title;
        [self.myImageView setImageWithURL:[NSURL URLWithString:_focusFetail.coverSmall] placeholderImage:[UIImage imageNamed:@"15.jpg"]];
        JingRoundView *jingRound = [JingRoundView defaultJingRoundView];
        [jingRound.roundImageView setImageWithURL:[NSURL URLWithString:_focusFetail.coverSmall] placeholderImage:[UIImage imageNamed:@"15.jpg"]];
    }
    [self handleUserData:[_focusFetail.myId integerValue]];
    
}

- (void)setAlbumPlay:(Album *)albumPlay
{
    if (![[albumPlay.albumId description] isEqualToString:[self.albumMore.trackId description]]) {
        NSString *str = [NSString stringWithFormat:@"http://mobile.ximalaya.com/mobile/track/detail?device=iPhone&trackId=%@", albumPlay.albumId];
        
        // 详情
        // http://mobile.ximalaya.com/mobile/track/detail?device=iPhone&trackId=4728193
        [NetHandleData getDataWithUrl:str completion:^(NSData *data) {
            id result = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
            self.albumMore = [[ComprehensiveAlbum alloc] initWithDictionary:result];
            [self.avPlayer pause];
            [self.avPlayer playWithUrl:[NSURL URLWithString:self.albumMore.playUrl64]];
            UIImage *image = [UIImage imageNamed:@"iconfont-zantinganniu.png"];
            [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
            // 把播放图标背景换成暂停图标
            [self.playButton setImage:image forState:UIControlStateNormal];
            // 设置按钮的实现方法
            [self.playButton addTarget:self action:@selector(pauseButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
            [self.myImageView setImageWithURL:[NSURL URLWithString:self.albumMore.coverLarge] placeholderImage:[UIImage imageNamed:@"15.jpg"]];
            JingRoundView *jingRound = [JingRoundView defaultJingRoundView];
            [jingRound.roundImageView setImageWithURL:[NSURL URLWithString:self.albumMore.coverLarge] placeholderImage:[UIImage imageNamed:@"15.jpg"]];
            self.nav.label.text = self.albumMore.title;
            self.playUrl = self.albumMore.playUrl64;
            self.userInfo = [[UserInfo alloc] initWithDictionary:self.albumMore.userInfo];
            [self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
            [self.tableView reloadData];

        
            
        }];
    }
    
    if (_albumPlay != albumPlay) {
        [_albumPlay release];
        _albumPlay = [albumPlay retain];
        self.urlStr = [NSString stringWithFormat:@"%@", _albumPlay.trackId];
    }
    [self handleData];
}

- (void)setHotPlay:(HotPresent *)hotPlay
{
    if (![self.playUrl isEqualToString:hotPlay.playUrl64]) {
        [self.avPlayer pause];
        [self.avPlayer playWithUrl:[NSURL URLWithString:hotPlay.playUrl64]];
        UIImage *image = [UIImage imageNamed:@"iconfont-zantinganniu.png"];
        [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        // 把播放图标背景换成暂停图标
        [self.playButton setImage:image forState:UIControlStateNormal];
        // 设置按钮的实现方法
        [self.playButton addTarget:self action:@selector(pauseButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
        
    }
    if (_hotPlay != hotPlay) {
        [_hotPlay release];
        _hotPlay = [hotPlay retain];
        self.playUrl = self.hotPlay.playUrl64;
        self.nav.label.text = self.hotPlay.title;
        [self.myImageView setImageWithURL:[NSURL URLWithString:_hotPlay.coverLarge]placeholderImage:[UIImage imageNamed:@"15.jpg"]];
        JingRoundView *jingRound = [JingRoundView defaultJingRoundView];
        [jingRound.roundImageView setImageWithURL:[NSURL URLWithString:_hotPlay.coverLarge] placeholderImage:[UIImage imageNamed:@"15.jpg"]];
    }
    [self handleUserData:[_hotPlay.trackId integerValue]];
    
}

- (void)handleUserData:(NSInteger)trackId
{
    NSString *str = [NSString stringWithFormat:@"http://mobile.ximalaya.com/mobile/track/detail?device=iPhone&trackId=%ld", (long)trackId];
    [NetHandleData getDataWithUrl:str completion:^(NSData *data) {
        id result = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        self.albumMore = [[ComprehensiveAlbum alloc] initWithDictionary:result];
        self.userInfo = [[UserInfo alloc] initWithDictionary:self.albumMore.userInfo];
        [self.tableView reloadData];
        [_userInfo release];
    }];
}

// 处理数据
- (void)handleData
{
    // 创建网络请求
    NSString *str = [NSString stringWithFormat:@"http://mobile.ximalaya.com/mobile/others/ca/album/track/%@/true/1/30?device=iPhone", self.hotPlay.albumId];
    
//    NSLog(@"%@", self.hotPlay.albumId);
    [NetHandleData getDataWithUrl:str completion:^(NSData *data) {
        
        // JSON解析
        id result = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        
//        self.album = [[AlbumTracks alloc] initWithDictionary:[result objectForKey:@"album"]];
        NSDictionary *tracksDic = [result objectForKey:@"tracks"];
        NSArray *listArr = [tracksDic objectForKey:@"list"];
        self.tracksArr = [NSMutableArray array];
        
        for (NSDictionary *tempDic in listArr) {
            AlbumTracks *album = [[AlbumTracks alloc] initWithDictionary:tempDic];
            [self.tracksArr addObject:album];
            [album release];
        }
        // 刷新页面
        [self.tableView reloadData];
//        [_album release];
    }];
    
    
}

- (void)viewWillDisappear:(BOOL)animated
{
    // 状态栏改成默认
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.integer = 1;
    if (self.tableView.contentOffset.y > self.view.bounds.size.height / 2) {
        // 将状态栏改为白色
        [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault];
    } else {
        // 将状态栏改为白色
        [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
    }
    [self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    NSLog(@"播放页面");
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playbackFinished:) name:AVPlayerItemDidPlayToEndTimeNotification object:self.avPlayer.currentItem];
    self.avPlayer = [AvReadioPlayer defaultAvPlayer];
    // 设置自定义tabBar
    // 设置背景颜色
    self.view.backgroundColor = [UIColor whiteColor];
    [self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    [self setImageViewAndPlayView];
    // 自定义导航栏
    [self setNavigation];
    // 设置播放器
    [self setPlayer];
    // 设置tableView
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height) style:UITableViewStylePlain];
    self.tableView.tableHeaderView = self.myImageView;
    self.tableView.backgroundColor = [UIColor lightGrayColor];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    self.tableView.contentInset = UIEdgeInsetsMake(0, 0, 49, 0);
    [self.view addSubview:self.tableView];
    [self.tableView registerClass:[DetailsCell class] forCellReuseIdentifier:REUSE];
    [self.tableView registerClass:[PlayCell class] forCellReuseIdentifier:kReuse];
    [self.tableView registerClass:[HotCell class] forCellReuseIdentifier:kHotReuse];
    [self.tableView registerClass:[FoThreeCell class] forCellReuseIdentifier:kFoThreeReuse];
    [self.tableView registerClass:[AlbumTableViewCell class] forCellReuseIdentifier:kAlbumCell];
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"abc"];
    UIImageView *back = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"tableViewback.jpg"]];
    self.tableView.backgroundView = back;
    [back release];
    [_tableView release];
    [self setTabBar];
    
    // 设置上切歌
    [self setUpSong];
    // 设置播放按钮
    [self setPlayButton];
    // 设置下切歌
    [self setDownSong];
    
    
    
    self.smallView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 20)];
    self.smallView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.smallView];
    self.smallView.hidden = YES;
    [_smallView release];
    

}
// 播放一首结束之后,自动切换下一曲
- (void)playbackFinished:(NSNotification *)notification
{
    [self setDownButtonAction];
}

// 设置上一首
- (void)setUpSong
{
    self.setUpButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.setUpButton.frame = CGRectMake(0, 0, self.playView.bounds.size.height - 30, self.playView.bounds.size.height - 30);
    self.setUpButton.center = CGPointMake((self.playView.bounds.size.width - 100) / 4, self.playView.bounds.size.height / 2);
    [self.setUpButton setImage:[[UIImage imageNamed:@"iconfont-shangyishou-2.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
    [self.setUpButton addTarget:self action:@selector(setUpButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.playView addSubview:self.setUpButton];
}

// 上一首方法
- (void)setUpButtonAction:(UIButton *)button
{
    NSLog(@"上一首");
    if (self.hotPlayArr.count != 0 && [[[self.hotPlayArr objectAtIndex:self.index] playUrl64] isEqualToString:self.hotPlay.playUrl64]) {
        if (self.index - 1 == 0) {
            NSString *string = @"第一曲";
            [self showAlertView:string];
           
        } else {
            [self setChangeSong:self.index - 1];
        }
    } else if (self.albumArr.count != 0 && [[[self.albumArr objectAtIndexedSubscript:self.index] playUrl64] isEqualToString:self.album.playUrl64]) {
        
        if (self.index == 0) {
            NSString *string = @"第一曲";
            [self showAlertView:string];
        } else {
            [self setChangeSongAlbum:self.index - 1];
        }
        
    } else if (self.focusFetailArr.count != 0 &&  [[[self.focusFetailArr objectAtIndex:self.index] playPath64] isEqualToString:self.focusFetail.playPath64]) {
        if (self.index == 0) {
            NSString *string = @"第一曲";
            [self showAlertView:string];
        } else {
            [self setChangeSongFocusFetail:self.index - 1];
        }
    } else {
        if (self.index == 0) {
            NSString *string = @"第一曲";
            [self showAlertView:string];
        }
    }

    
}

- (void)setChangeSongFocusFetail:(NSInteger)index
{
    self.index = index;
    self.focusFetail = [self.focusFetailArr objectAtIndex:self.index];
    self.nav.label.text = self.focusFetail.title;
    [self.myImageView setImageWithURL:[NSURL URLWithString:self.focusFetail.coverSmall] placeholderImage:[UIImage imageNamed:@"15.jpg"]];
    [self.avPlayer playWithUrl:[NSURL URLWithString:self.focusFetail.playPath64]];
    self.urlStr = self.focusFetail.playPath64;
    [self handleUserData:[_focusFetail.myId integerValue]];
}

- (void)setChangeSongAlbum:(NSInteger)index
{
    self.index = index;
    self.album = [self.albumArr objectAtIndex:self.index];
    self.nav.label.text = self.album.title;
    [self.myImageView setImageWithURL:[NSURL URLWithString:self.album.coverLarge] placeholderImage:[UIImage imageNamed:@"15.jpg"]];
    [self.avPlayer playWithUrl:[NSURL URLWithString:self.album.playUrl64]];
    self.urlStr = self.album.playUrl64;
}


// 设置下一首
- (void)setDownSong
{
    self.setDownButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.setDownButton.frame = CGRectMake(0, 0, self.playView.bounds.size.height - 30, self.playView.bounds.size.height - 30);
    self.setDownButton.center = CGPointMake((self.playView.bounds.size.width - 100) / 4 * 3, self.playView.bounds.size.height / 2);
    [self.setDownButton setImage:[[UIImage imageNamed:@"iconfont-xiayishou-2.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]forState:UIControlStateNormal];
    [self.setDownButton addTarget:self action:@selector(setDownButtonAction) forControlEvents:UIControlEventTouchUpInside];
    [self.playView addSubview:self.setDownButton];
}

// 下一首方法
- (void)setDownButtonAction
{
    NSLog(@"下一首");
    // 判断传过来的数组和下标与传过来的对象的音频路径是否一致
    if (self.hotPlayArr.count != 0 && [[[self.hotPlayArr objectAtIndex:self.index] playUrl64] isEqualToString:self.hotPlay.playUrl64]) {
        // 如果下标+1等于了数组的长度
        if (self.index + 1 == self.hotPlayArr.count) {
            NSString *str = @"最后一曲";
            // 提示已经是最后一首了
            [self showAlertView:str];
        } else {
            [self setChangeSong:self.index + 1];
        }
    } else if (self.albumArr.count != 0 && [[[self.albumArr objectAtIndexedSubscript:self.index] playUrl64] isEqualToString:self.album.playUrl64]) {
        
        if (self.index + 1 == self.albumArr.count) {
            NSString *string = @"最后一曲";
            [self showAlertView:string];
        } else {
            [self setChangeSongAlbum:self.index + 1];
        }
    } else if (self.focusFetailArr.count != 0 &&  [[[self.focusFetailArr objectAtIndex:self.index] playPath64] isEqualToString:self.focusFetail.playPath64]) {
        if (self.index + 1 == self.focusFetailArr.count) {
            NSString *string = @"最后一曲";
            [self showAlertView:string];
        } else {
            [self setChangeSongFocusFetail:self.index + 1];
        }
    } else {
        if (self.index == 0) {
            NSString *string = @"最后一曲";
            [self showAlertView:string];
        }
    }

}
// 提示最后一首
- (void)showAlertView:(NSString *)string
{
    NSString *str = [NSString stringWithFormat:@"亲, 已经是%@了哟", string];
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:str delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
    [alertView show];
    [alertView release];
}

// 赋值
- (void)setChangeSong:(NSInteger)index
{
    self.index = index;
    self.hotPlay = [self.hotPlayArr objectAtIndex:self.index];
    self.nav.label.text = self.hotPlay.title;
    [self.myImageView setImageWithURL:[NSURL URLWithString:self.hotPlay.coverLarge]placeholderImage:[UIImage imageNamed:@"15.jpg"]];
    [self.avPlayer playWithUrl:[NSURL URLWithString:self.hotPlay.playUrl64]];
    self.urlStr = self.hotPlay.playUrl64;
}

// 设置播放按钮
- (void)setPlayButton
{
    // 播放按钮
    self.playButton = [UIButton buttonWithType:UIButtonTypeCustom];
    // 设置按钮的大小
    self.playButton.frame = CGRectMake(0, 0, self.playView.bounds.size.height - 20, self.playView.bounds.size.height - 20);
    self.playButton.center = CGPointMake((self.playView.bounds.size.width - 100) / 2, self.playView.bounds.size.height / 2);
    UIImage *image = [UIImage imageNamed:@"iconfont-zantinganniu.png"];
    [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    // 把播放图标背景换成暂停图标
    [self.playButton setImage:image forState:UIControlStateNormal];
    // 设置按钮的实现方法
    [self.playButton addTarget:self action:@selector(pauseButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
    JingRoundView *jingRound = [JingRoundView defaultJingRoundView];
    jingRound.isPlay = YES;
    
    // 将按钮加载到视图上
    [self.playView addSubview:self.playButton];
    
}

// 自定义导航栏
- (void)setNavigation
{
    self.nav = [[MyNavigationView alloc] initWithFrame:CGRectMake(-1, 0, self.view.bounds.size.width + 1, 64)];
    self.nav.backgroundColor = [UIColor blackColor];
    self.nav.label.backgroundColor = [UIColor clearColor];
    self.nav.label.textColor = [UIColor whiteColor];
    if (self.hotPlay.title != nil) {
        self.nav.label.text = self.hotPlay.title;
    } else if (self.album.title != nil) {
        self.nav.label.text = self.album.title;
    } else if (self.focusFetail.title != nil) {
        self.nav.label.text = self.focusFetail.title;
    } else if (self.fileInfo.fileName != nil) {
        self.nav.label.text = self.fileInfo.fileName;
    }
    self.nav.label.font = [UIFont boldSystemFontOfSize:18];
    self.nav.label.frame = CGRectMake(0, 20, self.view.bounds.size.width, 44);
    [[self.nav layer] setBorderWidth:0];
    [self.myImageView addSubview:self.nav];
    self.nav.alpha = 0.6;
    [_nav release];
    
    
}


// 自定义tabBar
- (void)setTabBar
{
//    MyNavigationView *tabBar = [[MyNavigationView alloc] initWithFrame:CGRectMake(0, self.view.bounds.size.height - 49, self.view.bounds.size.width, 49)];
//    [self.view addSubview:tabBar];
//    [tabBar.button setTitle:@"关闭" forState:UIControlStateNormal];
//    [tabBar.button addTarget:self action:@selector(dismissButtonAction:) forControlEvents:UIControlEventTouchUpInside];
//    [tabBar release];
    
    // 自定义tabbar
    UIView *backView = [[UIView alloc] initWithFrame:CGRectMake(0, self.view.bounds.size.height - 49, self.view.bounds.size.width, 49)];
    [self.view addSubview:backView];
    backView.backgroundColor = [UIColor tabBarColor];
    // 返回键
    UIButton *backButton = [UIButton buttonWithType:UIButtonTypeSystem];
    [backView addSubview:backButton];
    backButton.frame = CGRectMake(10, 10, 30, 29);
    [backButton setImage:[UIImage imageNamed:@"iconfont-gerenfanhui.png"] forState:UIControlStateNormal];
    [backButton setTintColor:[UIColor sectionColor]];
    [backButton addTarget:self action:@selector(dismissButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    
}

- (void)dismissButtonAction:(UIButton *)button
{
    
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault];
    
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}




// 设置播放器
- (void)setPlayer
{
    // 播放器
    self.avPlayer = [AvReadioPlayer defaultAvPlayer];
    if (self.hotPlay.playUrl64 != nil) {
        [self.avPlayer playWithUrl:[NSURL URLWithString:self.hotPlay.playUrl64]];
    } else if (self.album.playUrl64 != nil) {
        [self.avPlayer playWithUrl:[NSURL URLWithString:self.album.playUrl64]];
    } else if (self.focusFetail.playPath64 != nil) {
        [self.avPlayer playWithUrl:[NSURL URLWithString:self.focusFetail.playPath64]];
    } else if (self.fileInfo.fileName != nil) {
        NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
        if ([self.fileInfo.fileName hasSuffix:@"aac"]) {
            NSString *filePath = [docPath stringByAppendingFormat: @"/Download/aac/%@", self.fileInfo.fileName];
            NSURL *url = [NSURL fileURLWithPath:filePath];
            [self.avPlayer playWithUrl:url];
            self.fileInfo = self.fileInfo;
            self.nav.label.text = self.fileInfo.fileName;
            [self.myImageView setImage:[UIImage imageNamed:@"15.jpg"]];
            
        } else if ([self.fileInfo.fileName hasSuffix:@"mp3"]) {
            NSString *filePath = [docPath stringByAppendingFormat: @"/Download/mp3/%@", self.fileInfo.fileName];
            NSURL *url = [NSURL fileURLWithPath:filePath];
            [self.avPlayer playWithUrl:url];
            self.fileInfo = self.fileInfo;
            self.nav.label.text = self.fileInfo.fileName;
            [self.myImageView setImage:[UIImage imageNamed:@"15.jpg"]];
        }

    }
    //设置初始音量大小
    self.avPlayer.volume = 1;
}
// 设置背景图片
- (void)setImageViewAndPlayView
{
    // 创建专辑详情播放视图并设置大小
    self.myImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, -20, self.view.bounds.size.width,self.view.bounds.size.width)];
    //    imageView.backgroundColor = [UIColor redColor];
    self.myImageView.backgroundColor = [UIColor whiteColor];
    self.myImageView.userInteractionEnabled = YES;
    JingRoundView *jingRound = [JingRoundView defaultJingRoundView];
    if (self.hotPlay.coverLarge != nil) {
        // 专辑详情播放视图赋值图片
        [self.myImageView setImageWithURL:[NSURL URLWithString:self.hotPlay.coverLarge] placeholderImage:[UIImage imageNamed:@"15.jpg"]];
        [jingRound.roundImageView setImageWithURL:[NSURL URLWithString:self.hotPlay.coverLarge] placeholderImage:[UIImage imageNamed:@"15.jpg"]];
    } else if (self.album.coverLarge != nil) {
        [self.myImageView setImageWithURL:[NSURL URLWithString:self.album.coverLarge] placeholderImage:[UIImage imageNamed:@"15.jpg"]];
        [jingRound.roundImageView setImageWithURL:[NSURL URLWithString:self.album.coverLarge] placeholderImage:[UIImage imageNamed:@"15.jpg"]];
    } else if (self.focusFetail.coverSmall != nil) {
        [self.myImageView setImageWithURL:[NSURL URLWithString:self.focusFetail.coverSmall] placeholderImage:[UIImage imageNamed:@"15.jpg"]];
        [jingRound.roundImageView setImageWithURL:[NSURL URLWithString:self.focusFetail.coverSmall] placeholderImage:[UIImage imageNamed:@"15.jpg"]];
    } else {
        [self.myImageView setImage:[UIImage imageNamed:@"15.jpg"]];
    }
    // 将专辑详情播放视图加载到视图上
    [self.view addSubview:self.myImageView];
    
    self.playView = [[UIView alloc] initWithFrame:CGRectMake(0, (1 - 84 / self.view.bounds.size.width) * self.view.bounds.size.width, self.view.bounds.size.width, 64)];
    self.playView.backgroundColor = [UIColor blackColor];
    self.playView.alpha = 0.7;
    [self.myImageView addSubview:self.playView];
    
    self.timerLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.playView.bounds.size.width - 100, 0, 100, self.playView.bounds.size.height)];
    self.timerLabel.textColor = [UIColor whiteColor];
    self.timerLabel.font = [UIFont boldSystemFontOfSize:15];
    self.timerLabel.text = @"00:00/00:00";
    [self.playView addSubview:_timerLabel];
    
    
    // 自定义滑条的图片
    UIImage *minImage = [UIImage imageNamed:@"faderTrack.png"];
    
    UIImage *maxImage = [UIImage imageNamed:@"grey_track.png"];
    
    minImage = [minImage stretchableImageWithLeftCapWidth:10.0 topCapHeight:0.0];
    
    maxImage = [maxImage stretchableImageWithLeftCapWidth:10.0 topCapHeight:0.0];
    
    // 自定义滑块的图片
    
    UIImage *tumbImage= [UIImage imageNamed:@"metal_screw.png"];
    
    
    
    self.timeValueSlider = [[UISlider alloc] initWithFrame:CGRectMake(0, (1 - 20 / self.view.bounds.size.width) * self.view.bounds.size.width, self.view.bounds.size.width, 20)];
    self.timeValueSlider.value = 0.0f;
    
//    [_timeValueSlider setMinimumTrackImage:minImage forState:UIControlStateNormal];
//    
//    [_timeValueSlider setMaximumTrackImage:maxImage forState:UIControlStateNormal];
    
    self.timeValueSlider.minimumTrackTintColor = [UIColor colorWithRed:68 / 255.0 green:150 / 255.0 blue:232 / 255.0 alpha:1];
    self.timeValueSlider.maximumTrackTintColor = [UIColor lightGrayColor];
    
    [_timeValueSlider setThumbImage:tumbImage forState:UIControlStateNormal];
    
    _timeValueSlider.continuous = YES;
    
    //注意这里要加UIControlStateHightlighted的状态，否则当拖动滑块时滑块将变成原生的控件
    [_timeValueSlider setThumbImage:tumbImage forState:UIControlStateHighlighted];
    
    [_timeValueSlider setThumbImage:tumbImage forState:UIControlStateNormal];
    
    [self.timeValueSlider addTarget:self action:@selector(changeTimerValue:) forControlEvents:UIControlEventValueChanged];
    self.timeValueSlider.backgroundColor = [UIColor whiteColor];
    [self.myImageView addSubview:_timeValueSlider];
    [NSTimer scheduledTimerWithTimeInterval:1.f target:self selector:@selector(changeTimer) userInfo:nil repeats:YES];
    
    [_timerLabel release];
    [_playView release];
    [_timeValueSlider release];
    // 内存管理
    [_myImageView release];
}
// 监听播放到多少秒
- (void)changeTimer
{
    // 时时监听
    //    self.player.currentItem.duration
    float value = CMTimeGetSeconds(self.avPlayer.currentItem.currentTime) / CMTimeGetSeconds(self.avPlayer.currentItem.duration);
    self.timeValueSlider.value = value;
    CGFloat timer = CMTimeGetSeconds(self.avPlayer.currentItem.duration);
    CGFloat timer1 = CMTimeGetSeconds(self.avPlayer.currentItem.currentTime);
    self.timerLabel.text = [NSString stringWithFormat:@"%@/%@", [self convertTime:timer1], [self convertTime:timer]];
    
}

// 将音频的秒数转换为分钟数
- (NSString *)convertTime:(CGFloat)second{
    NSDate *d = [NSDate dateWithTimeIntervalSince1970:second];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    if (second / 3600 >= 1) {
        [formatter setDateFormat:@"HH:mm:ss"];
    } else {
        [formatter setDateFormat:@"mm:ss"];
    }
    NSString *showtimeNew = [formatter stringFromDate:d];
    [formatter release];
    return showtimeNew;
}

- (void)changeTimerValue:(id)sender
{
    UISlider *slider = (UISlider *)sender;
    float value = slider.value;
    int32_t timer = self.avPlayer.currentItem.asset.duration.timescale;
    CGFloat timeValue = CMTimeGetSeconds(self.avPlayer.currentItem.duration) * value;
    [self.avPlayer seekToTime:CMTimeMakeWithSeconds(timeValue, timer) toleranceBefore:kCMTimeZero toleranceAfter:kCMTimeZero];
    CGFloat timer1 = CMTimeGetSeconds(self.avPlayer.currentItem.duration);
    self.timerLabel.text = [NSString stringWithFormat:@"%@/%@", [self convertTime:timeValue], [self convertTime:timer1]];
    [self.avPlayer play];
    
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (scrollView.contentOffset.y >= self.view.bounds.size.height / 2) {
        [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault animated:YES];
        self.smallView.hidden = NO;
    } else {
        [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent animated:YES];
        self.smallView.hidden = YES;
    }
    
}


// 播放按钮实现方法
- (void)playButtonClicked:(UIButton *)playButton
{
    //NSLog(@"播放");
    
    UIImage *image = [UIImage imageNamed:@"iconfont-zantinganniu.png"];
    [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    JingRoundView *jingRound = [JingRoundView defaultJingRoundView];
    jingRound.isPlay = YES;
    // 把播放图标背景换成暂停图标
    [playButton setImage:image forState:UIControlStateNormal];
    // 实现暂停按钮的方法
    [playButton addTarget:self action:@selector(pauseButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
    // 开始播放
    [self.avPlayer play];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        return 100;
    } else if (indexPath.section == 1) {
        NSString *title = self.albumMore.richIntro;
        NSDictionary *dic = @{NSFontAttributeName:[UIFont systemFontOfSize:13]};
        CGRect rect = [title boundingRectWithSize:CGSizeMake(self.view.bounds.size.width / 2, 10000) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
        return rect.size.height + 15;
    } else {
        
        if (self.hotPlayArr.count != 0) {
            NSString *title = [[self.hotPlayArr objectAtIndex:indexPath.section] title];
            NSDictionary *dic = @{NSFontAttributeName:[UIFont systemFontOfSize:17]};
            CGRect rect = [title boundingRectWithSize:CGSizeMake(self.tableView.bounds.size.width / 2, 10000) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
            CGFloat height = rect.size.height > (kCellWidth / 6) ? rect.size.height : (kCellWidth / 6);
            return height + 55;
        } else if (self.albumArr.count != 0) {
            NSString *title = [[self.albumArr objectAtIndex:indexPath.section] title];
            NSDictionary *dic = @{NSFontAttributeName:[UIFont systemFontOfSize:17]};
            CGRect rect = [title boundingRectWithSize:CGSizeMake(self.tableView.bounds.size.width / 2, 10000) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
            CGFloat height = rect.size.height > (kCellWidth / 6) ? rect.size.height : (kCellWidth / 6);
            return height + 55;
        } else if (self.focusFetailArr.count != 0) {
            NSString *title = [[self.focusFetailArr objectAtIndex:indexPath.section] title];
            NSDictionary *dic = @{NSFontAttributeName:[UIFont systemFontOfSize:17]};
            CGRect rect = [title boundingRectWithSize:CGSizeMake(self.tableView.bounds.size.width / 2, 10000) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
            CGFloat height = rect.size.height > (kCellWidth / 6) ? rect.size.height : (kCellWidth / 6);
            return height + 55;
        }
        
    }
    return 0;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 3;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UILabel *headLabel = [[UILabel alloc] init];
    if (section == 1) {
        headLabel.text = @"   详情";
    }
    if (section == 2) {
        headLabel.text = @"   专辑其他";
    }
    return headLabel;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    if (section == 1) {
        NSString *str = @" 详情";
        return str;
    }
    if (section == 2) {
        NSString *str = @" 专辑其他";
        return str;
    }
    return nil;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if ((section == 2 && (self.index + 1) != self.hotPlayArr.count && (self.index + 1) != self.focusFetailArr.count && (self.index + 1) != self.albumArr.count) && (self.hotPlayArr.count != 0 || self.focusFetailArr.count != 0 || self.albumArr != 0)) {
        return 1;
    } else if ((section == 1 || section == 0) && (self.focusFetail != nil|| self.album != nil || self.albumPlay != nil || self.hotPlay != nil) ) {
        return 1;
    } else {
        return 0;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        PlayCell *cell = [tableView dequeueReusableCellWithIdentifier:kReuse];
        [cell.myImageView setImageWithURL:[NSURL URLWithString:self.userInfo.smallLogo] placeholderImage:[UIImage imageNamed:@"15.jpg"]];
        
        cell.userName.text = self.userInfo.nickname;
        if ([self.userInfo.followers integerValue] >= 10000) {
            CGFloat index =  [self.userInfo.followers integerValue] / 10000.0;
            NSString *string = [NSString stringWithFormat:@"声音  %@   粉丝   %.1f万", self.userInfo.tracks, index];
            cell.numberLabel.text = string;
        } else {
            NSString *string = [NSString stringWithFormat:@"声音  %@   粉丝   %@", self.userInfo.tracks, self.userInfo.followers];
            cell.numberLabel.text = string;
        }
        cell.abstract.text = self.userInfo.personDescribe;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    } else if (indexPath.section == 1) {
        DetailsCell *cell = [tableView dequeueReusableCellWithIdentifier:REUSE];
        // JS代码
        // NSString *js = @"window.onload = function(){document.body.style.backgroundColor = '#4496e8';}";
        //        [cell.myWebView stringByEvaluatingJavaScriptFromString:js];
        // 隐藏之后, 不会出现黑条
        cell.myWebView.backgroundColor = [UIColor whiteColor];
        // 默认是肯定的。不透明的观点必须填补他们的整个范围或结果是未定义的活性CGContext drawRect:没有被清除,可能有非零像素
        cell.myWebView.opaque = NO;
        UIScrollView *scroll = [[cell.myWebView subviews] lastObject];
        scroll.scrollEnabled = NO;
        [cell.myWebView loadHTMLString:self.albumMore.richIntro baseURL:nil];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    } else {
        
        if (self.hotPlayArr.count != 0 && [[[self.hotPlayArr objectAtIndex:self.index] playUrl64] isEqualToString:self.hotPlay.playUrl64]) {
            HotCell *cell = [tableView dequeueReusableCellWithIdentifier:kHotReuse];
            cell.hotPre = [self.hotPlayArr objectAtIndex:self.index + self.integer];
            return cell;
        } else if (self.focusFetailArr.count != 0 &&  [[[self.focusFetailArr objectAtIndex:self.index] playPath64] isEqualToString:self.focusFetail.playPath64]) {
            FoThreeCell *cell = [tableView dequeueReusableCellWithIdentifier:kFoThreeReuse];
            cell.fFetail = [self.focusFetailArr objectAtIndex:self.index + self.integer];
            return cell;
        } else if (self.albumArr.count != 0 && [[[self.albumArr objectAtIndexedSubscript:self.index] playUrl64] isEqualToString:self.album.playUrl64]) {
            AlbumTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kAlbumCell];
            cell.tracks = [self.albumArr objectAtIndex:self.index + self.integer];
            return cell;
        }
        
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"abc"];
//        kFoThreeReuse
        return cell;
    }
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

// 暂停图标按钮实现的方法
- (void)pauseButtonClicked:(UIButton *)pauseButton
{
    //NSLog(@"暂停");
    
    UIImage *image = [UIImage imageNamed:@"iconfont-bofanganniu.png"];
    [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    // 将暂停图标换成播放图标
    [pauseButton setImage:image forState:UIControlStateNormal];
    JingRoundView *jingRound = [JingRoundView defaultJingRoundView];
    jingRound.isPlay = NO;
    // 实现播放按钮的方法
    [pauseButton addTarget:self action:@selector(playButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
    // 播放暂停
    [self.avPlayer pause];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
